package net.ak.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import net.ak.dao.UserCategoryDao;
import net.ak.model.Category;
import net.ak.model.User;
import net.ak.model.UserCategory;

@Repository
public class UserCategoryDaoImpl extends BaseDao<UserCategory> implements UserCategoryDao {

	/* (non-Javadoc)
	 * @see net.ak.dao.UserCategoryDao#listUserCategoryByListCategory(java.util.List)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<UserCategory> listUserCategoryByListCategory(List<Category> categories) {
		Session session = sessionFactory.openSession();
		Criteria criteria = session.createCriteria(UserCategory.class);
		for (Category catg : categories) {
			criteria.add(Restrictions.eq("category.id", catg.getId()));
		}
		List<UserCategory> userCategories = criteria.list();
		session.close();
		
		return userCategories;
	}

	/* (non-Javadoc)
	 * @see net.ak.dao.UserCategoryDao#listUserCategoryByUser(net.ak.model.User)
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<UserCategory> listUserCategoryByUser(User user) {
		if (user == null) return new ArrayList<UserCategory>();
		Session session 	= sessionFactory.openSession();
		Criteria criteria 	= session.createCriteria(UserCategory.class);
		criteria.add(Restrictions. eq("user.id", user.getId()));
		List<UserCategory> userCategories = criteria.list();
		session.close();
		
		return userCategories;
	}

	@Override
	public List<UserCategory> listUserCategoriesByCategoryId(Long catId) {
		Session session = sessionFactory.openSession();
		Criteria criteria = session.createCriteria(UserCategory.class);
		criteria.add(Restrictions.eq("category.id", catId));
		@SuppressWarnings("unchecked")
		List<UserCategory> userCategories = criteria.list();
		session.close();
		return userCategories;
	}
	
}
